﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;
using BusinessObjects;
using System.IO;

public partial class controls_admin_CategoryEdit : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            getSec_Admin_Modules();
            loadData();
        }
    }
    protected void loadData()
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = null;
            if (Request["m_id"] != null)
                objDT = BO_Sec_Admin_Modules.Search(Convert.ToInt32(Request["m_id"]));
            if (objDT != null && objDT.Rows.Count > 0)
            {
                txtMenuName.Text = objDT.Rows[0]["MODULE_NAME"].ToString();
                txtDesc.Text = objDT.Rows[0]["DESCRIPTION"].ToString();
                ddlParentMenu.SelectedValue = objDT.Rows[0]["PARENT_MODULE_ID"].ToString();
                cboDisOnHome.Checked = Convert.ToBoolean(objDT.Rows[0]["is_display"]);
                txtShortDisplay.Text = objDT.Rows[0]["SORT_DISPLAY"].ToString();
                if (!objDT.Rows[0]["THUMB_ICON"].ToString().Equals(String.Empty))
                    imgthumb.InnerHtml = "<img src='../resources/images/icons/" + objDT.Rows[0]["THUMB_ICON"].ToString() + "' alt='" + objDT.Rows[0]["MODULE_NAME"].ToString() + "' />";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void getSec_Admin_Modules()
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = BO_Sec_Admin_Modules.SelectParent(1, 0);
            ddlParentMenu.Items.Add(new ListItem("Chọn Menu", "0"));
            if (objDT.Rows.Count > 0)
            {
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    ddlParentMenu.Items.Add(new ListItem(objDT.Rows[i]["DESCRIPTION"].ToString(), objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()));
                    getChildSec_Admin_Modules(Convert.ToInt32(objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()), "");
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void getChildSec_Admin_Modules(int _parent_id, string ident)
    {
        try
        {
            DataTable objDT = BO_Sec_Admin_Modules.SelectParent(1, _parent_id);
            if (objDT.Rows.Count > 0)
            {
                ident += "--- ";
                for (int i = 0; i < objDT.Rows.Count; i++)
                {
                    ddlParentMenu.Items.Add(new ListItem(ident + objDT.Rows[i]["DESCRIPTION"].ToString(), objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()));
                    getChildSec_Admin_Modules(Convert.ToInt32(objDT.Rows[i]["ADMIN_MODULE_ID"].ToString()), ident);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        txtDesc.Text = "";
        txtMenuName.Text = "";
        cboDisOnHome.Checked = true;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            DataTable objDT = (DataTable)Session["admin"];
            if (Request["m_id"] != null)
            {
                DataTable dtData = BO_Sec_Admin_Modules.Search(Convert.ToInt32(Request["m_id"]));
                string filename = dtData.Rows[0]["THUMB_ICON"].ToString();
                try
                {
                    if (fulTHUMB_ICON.HasFile)
                    {
                        filename = fulTHUMB_ICON.FileName;

                        int count = 0;
                        while (File.Exists(Server.MapPath("~/resources/images/icons/") + filename))
                        {
                            filename = count + "_" + fulTHUMB_ICON.FileName;
                            count++;
                        }
                        fulTHUMB_ICON.SaveAs(Server.MapPath("~/resources/images/icons/") + filename);
                    }
                }
                catch (Exception ex)
                {
                    divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi Ghi file! " + ex.Message);
                    return;
                }
                string strResult = BO_Sec_Admin_Modules.Update(Convert.ToInt32(Request["m_id"]),
                    Convert.ToInt32(ddlParentMenu.SelectedValue),
                    txtMenuName.Text.Trim(), txtDesc.Text.Trim(), filename,
                    cboDisOnHome.Checked,
                    Convert.ToInt32(txtShortDisplay.Text.Trim()), 0,
                    DateTime.Now, Convert.ToDateTime(dtData.Rows[0]["CREATED_DATE"].ToString()),
                    dtData.Rows[0]["CREATED_BY"].ToString(), DateTime.Now,
                    objDT.Rows[0]["USER_NAME"].ToString(),
                    Boolean.Parse(dtData.Rows[0]["STATUS"].ToString()));
                if (strResult != "0")
                    divMessage.InnerHtml = O2S_Message.Success("Hệ Thống", "Đã cập nhật thông tin menu!");
                else
                    divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Có lỗi trong quá trình xử lý, thử lại sau!");
            }
            btnAdd_Click(null, null);
        }
        catch (Exception ex) { divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message); }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(12, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_PARENT":
                    NOTE_PARENT.InnerText = value;
                    break;
                case "NOTE_NAME":
                    NOTE_NAME.InnerText = value;
                    break;
                case "NOTE_TITLE":
                    NOTE_TITLE.InnerText = value;
                    break;
                case "NOTE_SORT":
                    NOTE_SORT.InnerText = value;
                    break;
                case "NOTE_ICON":
                    NOTE_ICON.InnerText = value;
                    break;
                case "NOTE_DISPLAY":
                    NOTE_DISPLAY.InnerText = value;
                    break;
            }
        }
    }
}
